﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

using BitmapTagger;

namespace ImageTagEditor.ValueConverters
{
	/// <summary>
	/// Converts resolution units to strings and back
	/// </summary>
	public class ResolutionUnitValueConverter : IValueConverter
	{
		/// <summary>
		/// Convert a resolution unit (ushort?) to it's string representation
		/// </summary>
		/// <param name="value">The resolution unit to convert</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not used</param>
		/// <returns>The string representation of the resolution unit</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return "";
			if (value is ushort?)
			{
				ushort? resUnit = value as ushort?;
				return ExifValues.Instance.GetResolutionUnitMeaning(resUnit);
			}
			throw new Exception("ResolutionUnitValueConverter can only convert objects of type ushort?");
		}

		/// <summary>
		/// Convert back from a string to a resolution unit (ushort?)
		/// </summary>
		/// <param name="value">The string representation of a resolution unit</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The resolution unit (ushort?) for the string if one exists</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is string)
			{
				string resString = value as string;
				return ExifValues.Instance.GetResolutionUnitValue(resString);
			}
			throw new Exception("ResolutionUnitValueConverter can only convert back from objects of type string");
		}

	}
}
